﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using Microsoft.SharePoint;
using System.Linq;
using System.Web.Script.Serialization;

namespace TIT.ABDI.ChamadosTI.VWPRelatorios2
{
    public partial class VWPRelatorios2UserControl : UserControl
    {
        SPWeb myWeb = SPContext.Current.Web;

        protected void Page_Load(object sender, EventArgs e)
        {
            DateTime now = DateTime.Now;
            DateTime dataInicial = new DateTime(now.Year, now.Month, 1);
            DateTime dataFinal = now;
            var serializer = new JavaScriptSerializer();
            List<Chamado> Chamados = myWeb.Lists["Chamado"].Items.Cast<SPListItem>()
                                    .Select(p => new Chamado()
                                    {
                                        Requerimento = p["Requerimento"] != null ? p["Requerimento"].ToString() : "",
                                        Criado = DateTime.Parse(p["Criado"].ToString()),
                                        CriadoPor = p["Criado por"] != null ? p["Criado por"].ToString().Split(new char[] { '#' }, StringSplitOptions.RemoveEmptyEntries)[1] : "",
                                        tipo_de_servico = p["tipo_de_servico"] != null ? p["tipo_de_servico"].ToString() : "",
                                    })
                                    .ToList();

            Chamados = Chamados.Where(c => c.CriadoPor == myWeb.CurrentUser.Name && c.Criado.Date >= dataInicial.Date && c.Criado.Date <= dataFinal.Date).ToList();

            var data = from u in Chamados
                   group u by u.Requerimento into g
                   select new Group { k = g.Key, v = g.Count() };

            div_data.InnerHtml = serializer.Serialize(data);
            RelatorioGrafico.Visible = data.Count() > 0;
        }
    }

    public class Group
    {
        public string k { get; set; }
        public int v { get; set; }
    }
}
